package com.thz.houserental.domain.requirement;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thz.houserental.infrastructure.auth.AuthUtil;
import com.thz.houserental.infrastructure.exception.ForbiddenException;
import com.thz.houserental.infrastructure.table.User;
import com.thz.houserental.infrastructure.web.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Tag(name = "用户需求管理")
@RequestMapping("/requirement")
public class RequirementController {
    private final RequirementService requirementService;
    private final UserService userService;

    public RequirementController(RequirementService requirementService, UserService userService) {
        this.requirementService = requirementService;
        this.userService = userService;
    }

    @Operation(description = "添加用户需求")
    @PostMapping("/add")
    public RequirementVo addRequirement(@RequestBody RequirementDto requirement) {
        return this.requirementService.addRequirement(requirement);
    }

    @Operation(description = "分页查询需求")
    @GetMapping("/list")
    public Page<RequirementVo> queryPageRequirement(RequirementVo requirementVo,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        return this.requirementService.queryPage(requirementVo, new Page<>(pageNo, pageSize));
    }

    //TODO 只能自己删或者管理员删
    @Operation(description = "删除用户需求")
    @DeleteMapping("/delete/{requirementId}")
    public void deleteRequirement(@PathVariable String requirementId) {
        // check 
        String currentUserId = AuthUtil.getCurrentUserId();
        Requirement requirement = this.requirementService.getById(requirementId);

        // TODO 判断角色，对管理员放行
        if (currentUserId.equals(requirement.getUserId())) {
            this.requirementService.removeById(requirementId);
        } else {
            throw new ForbiddenException(10600);
        }
    }

    @Operation(description = "更新需求")
    @PutMapping("/update/{requirementId}")
    public void updateRequirement(@RequestBody RequirementDto requirementDto, @PathVariable String requirementId) {
        // check
        String currentUserId = AuthUtil.getCurrentUserId();
        Requirement requirement = this.requirementService.getById(requirementId);

        // TODO 判断角色，对管理员放行
        if (currentUserId.equals(requirement.getUserId())) {
            BeanUtil.copyProperties(requirementDto, requirement);
            this.requirementService.updateById(requirement);
        } else {
            throw new ForbiddenException(10600);
        }
    }

    @GetMapping("/userAll")
    @Operation(description = "获取用户自己所有的需求")
    public List<RequirementVo> getUserRequirementByUserId() {
        String currentUserId = AuthUtil.getCurrentUserId();
        User user = userService.getById(currentUserId);
        List<Requirement> requirements = this.requirementService.lambdaQuery()
                .eq(Requirement::getUserId, currentUserId)
                .list();

        return requirements.stream()
                .map(e -> {
                    RequirementVo requirementVo = new RequirementVo();
                    BeanUtil.copyProperties(e, requirementVo);
                    requirementVo.setPhone(user.getPhone());
                    return requirementVo;
                })
                .collect(Collectors.toList());
    }

}
